//
//  BookMallViewController.m
//  LiteraryTune
//
//  Created by 神州四达 on 2018/1/15.
//  Copyright © 2018年 cxd. All rights reserved.
//  文曲经典书城

#import "BookMallViewController.h"
#import "TopCategoryShowView.h"
#import "BookTypeSearchView.h"
#import "MallHomePageViewController.h"
#import "MallBookViewController.h"
#import "MallListenViewController.h"
#import "PeriodicalViewController.h"
#import "MallNewspaperViewController.h"
#import "VoiceBookViewController.h"
#import "VideoVC.h"
#import "ReadAppBookFirendVC.h"
#import "HomeMenuModel.h"

@interface BookMallViewController () 

@property (nonatomic, strong)UIScrollView *backScrollView;
@property (nonatomic, strong)TopCategoryShowView *cateView;
@property (nonatomic, strong)BookTypeSearchView *searchView;
//首页
@property (nonatomic, strong)MallHomePageViewController *homeVC;
//图书
@property (nonatomic, strong)MallBookViewController *bookVC;
//期刊
@property (nonatomic, strong)PeriodicalViewController *periodicalVC;
//听书
@property (nonatomic, strong)MallListenViewController *listenVC;
//报纸
@property (nonatomic, strong)MallNewspaperViewController *paperVC;
//视频
@property (nonatomic, strong)VoiceBookViewController *mediaVC;
//音频
//@property (nonatomic, strong)VideoVC *videoViewC;

@property (nonatomic, strong) ReadAppBookFirendVC *friendVC;

@property (nonatomic, strong) NSMutableArray *menuTitles;

@end

@implementation BookMallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"阅美时光书城";
    [self loadMenuData];
}

-(void)loadMenuData{
//    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
//    NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
    
    [CXDNetHelp getRequestWithURLString:@"http://service.wenqujingdian.com/wqjd/app/GetModuleOfVersion" parameters:@{@"appId":@"ios",@"appType":@"ios",@"gradleVersion":@(2)} success:^(id  _Nullable responseObject) {
        
        NSArray *array = [NSArray yy_modelArrayWithClass:[HomeMenuModel class] json:responseObject[@"data"]];
        for (HomeMenuModel *model in array) {
            [self.menuTitles addObject:model.moduleName];
        }
        [self buildUI];

    } failure:^(NSError * _Nullable error) {
        
    }];
    
}

- (void)buildUI {
    [self.view addSubview:self.cateView];
    [self.view addSubview:self.backScrollView];
    
    self.backScrollView.contentSize = CGSizeMake(kScreenWidth, self.homeVC.view.height);
    [self addChildViewController:self.homeVC];
    [self.backScrollView addSubview:self.homeVC.view];
    
    [self addChildViewController:self.bookVC];
    [self addChildViewController:self.periodicalVC];
    [self addChildViewController:self.listenVC];
    [self addChildViewController:self.paperVC];
    [self addChildViewController:self.mediaVC];
    [self addChildViewController:self.friendVC];

//    [self addChildViewController:self.videoViewC];
    
    __weak typeof(self) WeakSelf = self;
    _bookVC.block = ^(CGFloat height) {
        WeakSelf.bookVC.view.height = height;
    };
    self.cateView.block = ^(NSString *nameString) {
        [WeakSelf.homeVC.view removeFromSuperview];
        [WeakSelf.bookVC.view removeFromSuperview];
        [WeakSelf.periodicalVC.view removeFromSuperview];
        [WeakSelf.listenVC.view removeFromSuperview];
        [WeakSelf.paperVC.view removeFromSuperview];
        [WeakSelf.mediaVC.view removeFromSuperview];
        [WeakSelf.friendVC.view removeFromSuperview];

//        [WeakSelf.videoViewC.view removeFromSuperview];
        
        if ([nameString isEqualToString:@"首页"]) {
            WeakSelf.backScrollView.contentSize = CGSizeMake(kScreenWidth, WeakSelf.homeVC.view.height);
            [WeakSelf.backScrollView addSubview:WeakSelf.homeVC.view];
        }else if ([nameString isEqualToString:@"阅享馆"]) {
            WeakSelf.backScrollView.contentSize = CGSizeMake(kScreenWidth, WeakSelf.bookVC.view.height);
            [WeakSelf.backScrollView addSubview:WeakSelf.bookVC.view];
        }else if ([nameString isEqualToString:@"期刊馆"]) {
            WeakSelf.backScrollView.contentSize = CGSizeMake(kScreenWidth, WeakSelf.periodicalVC.view.height);
            [WeakSelf.backScrollView addSubview:WeakSelf.periodicalVC.view];
//            [WeakSelf.periodicalVC reloadUrl];
        }else if ([nameString isEqualToString:@"听书馆"]) {
            WeakSelf.backScrollView.contentSize = CGSizeMake(kScreenWidth, WeakSelf.listenVC.view.height);
            [WeakSelf.backScrollView addSubview:WeakSelf.listenVC.view];
        }else if ([nameString isEqualToString:@"日习馆"]) {
            WeakSelf.backScrollView.contentSize = CGSizeMake(kScreenWidth, WeakSelf.paperVC.view.height);
            [WeakSelf.backScrollView addSubview:WeakSelf.paperVC.view];
//            [WeakSelf.paperVC reloadUrl];

        }else if ([nameString isEqualToString:@"体验馆"]) {
            WeakSelf.backScrollView.contentSize = CGSizeMake(kScreenWidth, WeakSelf.mediaVC.view.height);
            [WeakSelf.backScrollView addSubview:WeakSelf.mediaVC.view];
        }else if ([nameString isEqualToString:@"书友会"]){
            WeakSelf.backScrollView.contentSize = CGSizeMake(kScreenWidth, WeakSelf.friendVC.view.height);
            [WeakSelf.backScrollView addSubview:WeakSelf.friendVC.view];
        }
//        else if ([nameString isEqualToString:@"音频"]) {
//            WeakSelf.backScrollView.contentSize = CGSizeMake(kScreenWidth, WeakSelf.videoViewC.view.height);
//            [WeakSelf.backScrollView addSubview:WeakSelf.videoViewC.view];
//        }
    };
}


#pragma mark - event response
- (void)tapClickAction {
    [self.searchView.nameTF resignFirstResponder];
}

#pragma mark - 懒加载
- (UIScrollView *)backScrollView {
    if (!_backScrollView) {
        _backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.cateView.bottom, kScreenWidth, kScreenHeight - CXDTopHeight - 49)];
        _backScrollView.showsVerticalScrollIndicator = NO;
    }
    return _backScrollView;
}

- (TopCategoryShowView *)cateView {
    if (!_cateView) {
        _cateView = [[TopCategoryShowView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
        _cateView.titleArray = self.menuTitles.copy; //@[@"首页",@"图享馆",@"期刊馆",@"听书馆",@"日习馆",@"体验馆",@"书友会"];//self.menuTitles.copy;
    }
    return _cateView;
}

- (BookTypeSearchView *)searchView {
    if (!_searchView) {
        _searchView = [[BookTypeSearchView alloc] initWithFrame:CGRectMake(0, self.cateView.bottom + 10, kScreenWidth, 60)];
    }
    return _searchView;
}
//首页 1190 + 65
- (MallHomePageViewController *)homeVC {
    if (!_homeVC) {
        _homeVC = [[MallHomePageViewController alloc] init];
        _homeVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight - 49 - 50);
    }
    return _homeVC;
}
//图书
- (MallBookViewController *)bookVC {
    if (!_bookVC) {
        _bookVC = [[MallBookViewController alloc] init];
        _bookVC.view.frame = CGRectMake(0, 0, kScreenWidth, 1250);
    }
    return _bookVC;
}
//期刊
- (PeriodicalViewController *)periodicalVC {
    if (!_periodicalVC) {
        _periodicalVC = [[PeriodicalViewController alloc] init];
        _periodicalVC.view.frame = CGRectMake(0, 0, kScreenWidth, 1070);
    }
    return _periodicalVC;
}

//听书
- (MallListenViewController *)listenVC {
    if (!_listenVC) {
        _listenVC = [[MallListenViewController alloc] init];
        _listenVC.view.frame = CGRectMake(0, 0, kScreenWidth, 905);
    }
    return _listenVC;
}
//报纸
- (MallNewspaperViewController *)paperVC {
    if (!_paperVC) {
        _paperVC = [[MallNewspaperViewController alloc] init];
        _paperVC.view.frame = CGRectMake(0, 0, kScreenWidth, 705);
    }
    return _paperVC;
}

- (VoiceBookViewController *)mediaVC {
    if (!_mediaVC) {
        _mediaVC = [[VoiceBookViewController alloc] init];
        _mediaVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight - 49 - 60);
    }
    return _mediaVC;
}
-(ReadAppBookFirendVC *)friendVC{
    if (!_friendVC) {
        _friendVC = [[ReadAppBookFirendVC alloc] init];
        _friendVC.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - CXDTopHeight - 49 - 60);

    }
    return _friendVC;
}
-(NSMutableArray *)menuTitles{
    if (!_menuTitles) {
        _menuTitles = [[NSMutableArray alloc] init];
    }
    return _menuTitles;
    
}
@end
